package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacRoleUser;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.role.RoleBindUserDto;
import com.csun.cmny.provider.model.dto.role.RoleBindUserReqDto;
import com.csun.cmny.provider.model.dto.user.UserQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacRoleService;
import com.csun.cmny.provider.service.UacRoleUserService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色绑定用户.
 *
 * @author paascloud.net @gmail.com
 */
@RestController
@RequestMapping(value = "/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - UacRoleBindUserController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacRoleBindUserController extends BaseController {

	@Resource
	private CmnyMessageSource cmnyMessageSource;

	@Resource
	private UacRoleService uacRoleService;

	@Resource
	private UacRoleUserService uacRoleUserService;

	/**
	 * 角色绑定用户.
	 *
	 * @param roleBindUserReqDto the role bind user req dto
	 *
	 * @return the wrapper
	 */
	//@LogAnnotation
	@PostMapping(value = "/bindUser")
	public Wrapper bindUser(@RequestBody RoleBindUserReqDto roleBindUserReqDto) {

		LoginAuthDto loginAuthDto = getLoginAuthDto();
		uacRoleService.bindUser4Role(roleBindUserReqDto, loginAuthDto);

		//return WrapMapper.ok();
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null));
	}

	/**
	 * 获取角色绑定用户页面数据.
	 *
	 * @param roleId the role id
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/getBindUser/{roleId}")
	@ApiOperation(httpMethod = "POST", value = "获取角色绑定用户页面数据")
	public Wrapper<RoleBindUserDto> getBindUser(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId) {
		logger.info("获取角色绑定用户页面数据. roleId={}", roleId);
		LoginAuthDto loginAuthDto = super.getLoginAuthDto();
		Long currentUserId = loginAuthDto.getUserId();
		RoleBindUserDto bindUserDto = uacRoleService.getRoleBindUserDto(roleId, currentUserId);
		//return WrapMapper.ok(bindUserDto);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), bindUserDto);
	}

	/**
	 * 查询用户列表（含与角色Id的绑定状态）
	 *
	 * @param userQueryDto	the user query dto
	 *
	 * @return	the wrapper
	 */
	@PostMapping(value = "/queryBindUserList")
	public Wrapper queryBindUserList(@RequestBody UserQueryDto userQueryDto) {

		userQueryDto.setPageSize(65535);
		List<UacUser> uacUserList = uacRoleUserService.queryBindRoleUserList(userQueryDto);

		//return WrapMapper.ok(uacUserList);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), uacUserList);
	}

	/**
	 * 查询角色用户列表
	 *
	 * @param userQueryDto	the user query dto
	 *
	 * @return	the wrapper
	 */
	@PostMapping(value = "/queryRoleUserListWithPage")
	public Wrapper queryRoleUserListWithPage(@RequestBody UserQueryDto userQueryDto) {

		PageHelper.startPage(userQueryDto.getPageNum(), userQueryDto.getPageSize());

		List<UacUser> uacUserList = uacRoleUserService.queryRoleUserList(userQueryDto);

		//return WrapMapper.ok(new PageInfo<>(uacUserList));
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), new PageInfo<>(uacUserList));
	}

	/**
	 * 移除单个角色用户
	 *
	 * @param uacRoleUser
	 * @return
	 */
	@PostMapping(value = "/singleDeleteUser")
	public Wrapper singleDeleteRoleUser(@RequestBody UacRoleUser uacRoleUser) {

		Integer result = uacRoleUserService.delete(uacRoleUser);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 批量移除角色用户
	 *
	 * @param roleBindUserReqDto	the role bind user req dto
	 *
	 * @return	the int.
	 */
	@PostMapping(value = "/batchDeleteUser")
	public Wrapper batchDeleteRoleUser(@RequestBody RoleBindUserReqDto roleBindUserReqDto) {

		Integer result = uacRoleUserService.batchDeleteRoleUser(roleBindUserReqDto);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

//	@PostMapping(value = "/assignUser")
//	public Wrapper assignRoleUser(@RequestBody RoleAssignUserDto roleAssignUserDto) {
//
//		Integer result = uacRoleUserService.assignRoleUser(roleAssignUserDto);
//		if (result < 1) {
//			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
//					.getMessage("operation.failed", null));
//		}
//
//		return WrapMapper.ok(result);
//	}

}
